﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mock_ehr_web.Models;
using Order = mock_ehr_lib.Model.OrderEntry.Order;

namespace mock_ehr_web.Controllers
{
    public class OrderController : Controller
    {
        //
        // GET: /Order/

        public ActionResult Index()
        {
            return PartialView();
        }

        public ActionResult Radiology()
        {
            var app = this.GetApp();
            var orders = app.OrderRepository.GetOrdersByPatientID(app.CurrentPatient.ID);
            return PartialView("Radiology", orders.Where(i => i.Type == "Radiology")); // naughty
        }

        public ActionResult Medications()
        {
            var app = this.GetApp();
            var orders = app.OrderRepository.GetOrdersByPatientID(app.CurrentPatient.ID);
            return PartialView("MedicationList", orders.Where(i => i.Type == "Medication"));
        }

        public ActionResult Create(string nqfNo)
        {
            var app = this.GetApp();
            var order = new Order
                            {
                                OrderID = Guid.NewGuid().ToString(),
                                PatientID = app.CurrentPatient.ID,
                                Status = "New",
                                EndDate = DateTime.Now.Add(TimeSpan.FromDays(1))
                            };
             
            if(nqfNo == "0043")
            {
                order.Type = "Medication";
                order.Description = "Pneumovax";
            }
            else if (nqfNo == "0027")
            {
                order.Type = "Nursing";
                order.Description = "Smoking cessation counseling";
            }
            return PartialView("Create", order);
        }

        public ActionResult Nursing()
        {
            var app = this.GetApp();
            var orders = app.OrderRepository.GetOrdersByPatientID(app.CurrentPatient.ID);
            return PartialView("Nursing", orders.Where(i => i.Type == "Nursing")); // naughty
        }

        public ActionResult Submit(Order order)
        {
            var app = this.GetApp();
            var patientID = app.CurrentPatient.ID;
            order.PatientID = patientID;
            app.OrderRepository.SubmitOrder(order);
            return PartialView("Success", new SuccessModel
                                              {
                                                  Message = "Order for " + order.Description + " created successfully",
                                                  ViewUrl = "#order-entry"
                                              });
        }
    }
}
